<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>餐厅扫码点餐系统 - SmartOrder</title>
    <link rel="stylesheet" href="/css/element-ui.css">
    <style>
        body {
            font-family: Avenir, Helvetica, Arial, sans-serif;
            color: #2c3e50;
        }

        a {
            color: #2c3e50;
            text-decoration: none;
        }

        a:hover {
            color: #409eff;
            text-decoration: none;
        }

        .footer {
            margin-top: 20px;
            background-color: #f56c6c;
            color: white;
            font-size: 18px;
            line-height: 54px;
            text-align: center;
        }
    </style>
</head>
<body>
<div id="app" style="text-align: center;">
    <h1>餐厅扫码点餐系统（SmartOrder）后台管理</h1>
    <div style="margin-top: 20px;">
        <a href="dining_table.html">餐桌管理</a> |
        <a href="category.html">菜品类别管理</a> |
        <a href="dish.html">菜品管理</a> |
        <a href="check_out.html">结账管理</a> |
        <a href="/">返回前台</a>
    </div>
    <div style="margin-top: 20px;">
        <el-select v-model="selectedTableId" placeholder="请选择餐桌" @change="fetchOrderInfoList">
            <el-option
                    v-for="table in diningTables"
                    :key="table.id"
                    :label="table.id + '桌'"
                    :value="table.id">
            </el-option>
        </el-select>
    </div>
    <div style="width: 70%; margin: 0 auto;">
        <el-table
                :data="orderInfoList"
                border
                style="width: 100%; margin-top: 20px;">
            <el-table-column
                    prop="id"
                    label="ID"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="dishName"
                    label="菜品"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="dishPrice"
                    label="单价"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="dishQuantity"
                    label="数量"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="合计"
                    align="center">
                <template slot-scope="scope">
                    {{ scope.row.dishPrice * scope.row.dishQuantity }}
                </template>
            </el-table-column>
            <el-table-column
                    label="管理"
                    align="center">
                <template slot-scope="scope">
                    <el-button type="danger" icon="el-icon-delete" circle size="mini"
                               @click="deleteOrder(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <div style="margin-top: 20px; margin-bottom: 20px;">
        <span style="color: red;">订单总价：{{ totalPrice.toFixed(2) }}元</span>
    </div>
    <div style="margin-top: 20px;">
        <el-button type="primary" :disabled="orderInfoList.length === 0" @click="checkout">结账</el-button>
    </div>
</div>
<div class="footer">
    当前页面由AI自动生成，可能需要进一步优化或调整。
</div>
<!-- 引入Vue.js -->
<script src="/js/vue.js"></script>
<!-- 引入Element-UI -->
<script src="/js/element-ui.js"></script>
<!-- 引入AXIOS -->
<script src="/js/axios.min.js"></script>
<script>
    console.log('页面加载中...');
    new Vue({
        el: '#app',
        data: {
            diningTables: [],
            selectedTableId: null,
            orderInfoList: []
        },
        created() {
            this.fetchDiningTables();
        },
        computed: {
            totalPrice() {
                return this.orderInfoList.reduce((total, item) => total + item.dishPrice * item.dishQuantity, 0);
            }
        },
        methods: {
            fetchDiningTables() {
                console.log('获取餐桌列表...');
                axios.get('/api/diningTables')
                    .then(response => {
                        console.log('获取餐桌列表成功:', response.data);
                        this.diningTables = response.data;
                    })
                    .catch(error => {
                        console.error('获取餐桌列表失败:', error);
                    });
            },
            fetchOrderInfoList() {
                console.log('获取订单列表...');
                axios.get(`/api/orderInfo/listByTableId?tableId=${this.selectedTableId}`)
                    .then(response => {
                        console.log('获取订单列表成功:', response.data);
                        this.orderInfoList = response.data;
                    })
                    .catch(error => {
                        console.error('获取订单列表失败:', error);
                    });
            },
            checkout() {
                console.log('结账...');
                this.$confirm('确认结账吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(`/api/orderInfo/checkOut?tableId=${this.selectedTableId}`)
                        .then(response => {
                            console.log('结账结果:', response.data);
                            if (response.data > 0) {
                                this.$message({
                                    type: 'success',
                                    message: '结账成功!'
                                });
                            } else {
                                this.$message.error('结账失败!');
                            }
                            this.fetchOrderInfoList();
                        })
                        .catch(error => {
                            console.error('结账失败:', error);
                        });
                }).catch(() => {
                    console.log('取消结账');
                });
            },
            deleteOrder(orderId) {
                console.log('删除订单:', orderId);
                this.$confirm('确认删除订单中的这条数据吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(`/api/orderInfo/delete?orderInfoId=${orderId}`)
                        .then(response => {
                            console.log('删除订单结果:', response.data);
                            if (response.data === 1) {
                                this.$message({
                                    type: 'success',
                                    message: '删除成功'
                                });
                            } else {
                                this.$message.error('删除失败');
                            }
                            this.fetchOrderInfoList(); // 重新获取订单列表
                        })
                        .catch(error => {
                            console.error('删除订单失败:', error);
                            this.$message.error('删除失败');
                        });
                }).catch(() => {
                    console.log('取消删除');
                });
            }
        }
    });
</script>
</body>
</html>